package com.mall.service.impl;

import com.mall.pojo.goods.Sku;
import com.mall.pojo.order.OrderItem;
import com.mall.service.goods.SkuService;
import com.mall.service.order.CartService;
import com.mall.util.CacheKey;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@DubboService
public class CartServiceImpl implements CartService {

    @Autowired
    private RedisTemplate redisTemplate;

    @DubboReference
    SkuService skuService;

    @Override
    public List<Map<String, Object>> findCartList(String username) {
        System.out.println("从redis中提取购物车"+username);
        List<Map<String,Object>> cartList = (List<Map<String,Object>>) redisTemplate.boundHashOps(CacheKey.CART_LIST).get(username);
        if(cartList==null){
            return new ArrayList<>();
        }
        return cartList;
    }

    @Override
    public void addItem(String username, String skuId, Integer num) {
        //获取购物车
        List<Map<String, Object>> cartList = findCartList(username);
        boolean flag =false;//是否是购物车中的商品
        //判断缓存中是含有已购物商品
        for (Map<String, Object> map : cartList) {
            OrderItem orderItem = (OrderItem) map.get("item");//提取购物车
            if(orderItem.getSkuId().equals(skuId)){//如果在购物车中找到
                //计算单个商品重量
                int weight = orderItem.getWeight() / orderItem.getNum();//单个数量
                orderItem.setNum(orderItem.getNum()+num);//数量增加
                orderItem.setWeight(orderItem.getNum()*weight);//重量计算
                orderItem.setMoney(orderItem.getNum()+orderItem.getPrice());//金额
                //如果购物数量小于等于0说明客户不购买了,应删除购物明细
                if (orderItem.getNum()<=0){
                    cartList.remove(map);
                }
                flag=true;
                break;
            }
        }
        if (flag == false) {//如果没找到,则增加商品到购物车
            //添加商品
            Sku sku = skuService.findById(skuId);
            if (sku ==null){
                throw new RuntimeException("商品不存在");
            }
            if(!sku.getStatus().equals("1")){
                throw new RuntimeException("商品状态不合法");
            }
            //数量不能为0或负数
            if(num<=0){
                throw new RuntimeException("商品数量不合法");
            }
            OrderItem orderItem=new OrderItem();
            orderItem.setCategoryId3(sku.getCategoryId());//分类id
            orderItem.setSpuId(sku.getSpuId());
            orderItem.setSkuId(sku.getId());
            orderItem.setNum(num);
            orderItem.setImage(sku.getImage());
            orderItem.setPrice(sku.getPrice());
            orderItem.setName(sku.getName());
            orderItem.setMoney(orderItem.getPrice()*num);
            if (sku.getWeight()==null){
                sku.setWeight(0);
            }
            orderItem.setWeight(sku.getWeight()*num);//计算重量
            Map<String,Object> map =new HashMap<>();
            map.put("item",orderItem);
            map.put("checked",true);//默认选中
            cartList.add(map);
        }
        redisTemplate.boundHashOps(CacheKey.CART_LIST).put(username,cartList);//存入缓存
    }

    @Override
    public boolean updateChecked(String username, String skuId, boolean checked) {
        return false;
    }

    @Override
    public int preferential(String username) {
        return 0;
    }

    @Override
    public List<Map<String, Object>> findNewOrderItemList(String username) {
        return (List<Map<String, Object>>)redisTemplate.boundHashOps(CacheKey.CART_LIST).get(username);
    }

    @Override
    public void deleteCheckedCart(String username) {

    }

    @Override
    public void deleteSkuIds(String username, String sukId) {

    }
}
